package com.lp.service.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.iframework.commons.util.fast.L;
import org.iframework.commons.util.fast.V;
import org.iframework.support.spring.cache.api.BaseCacheManager;
import org.iframework.support.spring.controller.BaseControllerSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lp.business.domain.Result;
import com.lp.business.function.WXUtil;
import com.lp.business.model.Member;
import com.lp.business.service.MemberService;

/**
 * 登录注册
 *
 */
@RestController
@RequestMapping("/api/member")
public class MemberController extends BaseControllerSupport{

	@Autowired
	private MemberService memberService;
	@Resource(name = "baseCacheManager")
	private BaseCacheManager cacheManager;
	
	/**
	 * 用户登陆
	 */
	@RequestMapping(value = "test",method = { RequestMethod.GET,RequestMethod.POST })
	public Result test(HttpServletRequest request, HttpServletResponse response) {
		L.i("test");
		return Result.ok().put("data","test");
		
	}
	/**
	 * 用户登陆
	 */
	@RequestMapping(value = "wxLogin",method = { RequestMethod.GET,RequestMethod.POST })
	public Result wxLogin(HttpServletRequest request, HttpServletResponse response,String code) {
		String openId =WXUtil.getOpenIdByWXCode(code);
		L.i("wxLogin,openId为："+openId);
		if(V.isEmpty(openId)) {
			return Result.error("微信登录失败");
		}
		Member member = memberService.findByOpenId(openId);
		if(V.isEmpty(member)) {
			member = new Member();
			member.setOpenId(openId);
			memberService.save(member);
		}
		cacheManager.cacheObject("token_"+openId, openId, 30 * 60L);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("token", openId);
		return Result.ok("微信登录成功").put("data",data);
	}
	
	/**
	 * 补充用户信息 <br>
	 */
	@RequestMapping(value = "renew", method = { RequestMethod.POST, RequestMethod.GET })
	public Result renew(HttpServletRequest request, final HttpServletResponse response, @RequestBody Map<String,String> params) {
		try {
			String memberId = (String)request.getAttribute("memberId");
			if(V.isEmpty(memberId)) {
				return Result.error("登录状态失效");
			}
			Member member = memberService.get(memberId);
			if(V.isEmpty(member)) {
				return Result.error("登录状态失效");
			}
			String userName = params.get("userName");
			String career = params.get("career");
			String mobile = params.get("mobile");
			String email = params.get("email");
			
			if(V.isNotEmpty(userName)){
				member.setUserName(userName);
			}
			if(V.isNotEmpty(career)){
				member.setCareer(career);
			}
			if(V.isNotEmpty(mobile)&&V.isMobile(mobile)){
				member.setMobile(mobile);
			}
			if(V.isNotEmpty(email)&&V.isEmail(mobile)){
				member.setEmail(email);
			}
			
			if(memberService.update(member)){
				Map<String, Object> data = new HashMap<String, Object>();
				data.put("member", member);
				return Result.ok().put("data", data);
			}else{
				return Result.error("信息完善失败");
			}
		} catch (Exception e) {
			L.e(e.getMessage(), e);
			return Result.error("接口异常");
		}
	}
	
}
